﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Function;

namespace UI.UserControl.Base
{
    /// <summary>
    /// 搜索
    /// </summary>
    public class QueryBase
    {
        /// <summary>
        /// 搜索类型
        /// </summary>
        public enum SearchType
        {
            /// <summary>
            /// 包含关键字
            /// </summary>
            Contain = 0,

            /// <summary>
            /// 等于关键字
            /// </summary>
            Equal = 1,

            /// <summary>
            /// 不包括
            /// </summary>
            Except = 2
        }

        /// <summary>
        /// span红色字体
        /// </summary>
        private string spanTemplate = "<span style='color:red'>{0}</span>";

        /// <summary>
        /// 搜索提示信息开始文字
        /// </summary>
        private string startSearchWord = "搜索条件: ";

        /// <summary>
        /// 默认sql搜索条件
        /// </summary>
        public string SqlDefaultCondtion = "1=1 ";

        /// <summary>
        /// sql搜索条件
        /// </summary>
        public string SqlCondition { get; set; }

        /// <summary>
        /// 搜索提示文字
        /// </summary>
        public string SearchTip { get; set; }

        /// <summary>
        /// 搜索关键字
        /// </summary>
        public List<QueryWord> Words { get; set; }

        /// <summary>
        /// 当前请求
        /// </summary>
        private HttpRequest requset = HttpContext.Current.Request;

        /// <summary>
        /// 初始化数据得到搜索条件和提示关键字
        /// </summary>
        public void InitData()
        {
            if (SqlDefaultCondtion.IsNotEmpty())
            {
                SqlCondition = SqlDefaultCondtion;
            }
            if (Words != null)
            {
                foreach (var item in Words)
                {
                    if (item.SearchValue.IsEmpty())
                        continue;
                    var tiptype = "";
                    //搜索类型
                    switch (item.SearchType)
                    {
                        case SearchType.Contain:
                            SqlCondition += string.Format(" and {0} like '%{1}%'", item.SearchKey, item.SearchValue);
                            tiptype = "包含";
                            break;
                        case SearchType.Equal:
                            if (item.ValueWithBarce)
                                SqlCondition += string.Format(" and {0} = '{1}'", item.SearchKey, item.SearchValue);
                            else
                                SqlCondition += string.Format(" and {0} = {1}", item.SearchKey, item.SearchValue);
                            tiptype = "等于";
                            break;
                        case SearchType.Except:
                            if (item.ValueWithBarce)
                                SqlCondition += string.Format(" and {0} <> '{1}'", item.SearchKey, item.SearchValue);
                            else
                                SqlCondition += string.Format(" and {0} <> '{1}'", item.SearchKey, item.SearchValue);
                            tiptype = "不包含";
                            break;
                    }
                    //搜索提示
                    if (item.DisplaySearchInfo)
                    {
                        SearchTip += string.Format("{0}{1}[{2}];", item.WordTip, tiptype, string.Format(spanTemplate, item.ValueTip));
                    }
                }
            }
            if (SearchTip.IsNotEmpty())
            {
                SearchTip = startSearchWord + SearchTip;
                if (SearchTip.EndsWith(";"))
                    SearchTip = SearchTip.Substring(0, SearchTip.Length - 1);
            }
        }

        /// <summary>
        /// 设置搜索关键字
        /// </summary>
        /// <param name="searchKey">搜索的数据库字段</param>
        /// <param name="wordTip">关键字提示</param>
        /// <param name="valueTip">关键字提示的搜索值 为空则等于SearchValue</param>
        /// <param name="searchValue">搜索的值</param>
        /// <param name="displaySearchInfo">是否显示该字段搜索提示信息</param>
        /// <param name="valueWithBarce">关键字值是否用括号包住 适用于字符串 对Contain搜索类型的值无用</param>
        /// <param name="searchType">搜索类型</param>
        /// <returns></returns>
        public QueryWord SetQueryWord(string searchKey, string wordTip, string searchValue = "", string valueTip = "", bool displaySearchInfo = true, bool valueWithBarce = true, SearchType searchType = SearchType.Equal)
        {

            //关键字searchKey 如果searchValue为空 默认情况下通过该值来获取request中要求筛选的值searchValue
            if (searchValue.IsEmpty())
                searchValue = requset.QueryString[searchKey];
            //valueTip 为空则先尝试获取request中的searchKey加上tip的值是否存在 
            //没有默认等于searchValue 对于dropdownlist等后台查询id 前台需要显示文字的 则需单独设置值
            if (valueTip.IsEmpty())
            {
                if (requset.QueryString[searchKey + "tip"].IsNotEmpty())
                    valueTip = requset.QueryString[searchKey + "tip"];
                else
                    valueTip = searchValue;
            }
            //valueWithBarce 是否用单引号包住关键字的值 对于Contain搜索类型的值由于采用like拼接 因此会被忽略
            QueryWord word = new QueryWord { DisplaySearchInfo = displaySearchInfo, SearchKey = searchKey, SearchType = searchType, SearchValue = searchValue, ValueTip = valueTip, ValueWithBarce = valueWithBarce, WordTip = wordTip };
            return word;
        }
    }

    public class QueryWord
    {
        /// <summary>
        /// 搜索的数据库字段
        /// </summary>
        public string SearchKey { get; set; }

        /// <summary>
        /// 关键字提示
        /// </summary>
        public string WordTip { get; set; }

        /// <summary>
        /// 关键字提示的搜索值 为空则等于SearchValue
        /// </summary>
        public string ValueTip { get; set; }

        /// <summary>
        /// 搜索的值
        /// </summary>
        public string SearchValue { get; set; }

        /// <summary>
        /// 是否显示该字段搜索提示信息
        /// </summary>
        public bool DisplaySearchInfo { get; set; }

        /// <summary>
        /// 关键字值是否用括号包住 适用于字符串 对Contain搜索类型的值无用
        /// </summary>
        public bool ValueWithBarce { get; set; }

        /// <summary>
        /// 搜索类型
        /// </summary>
        public QueryBase.SearchType SearchType { get; set; }
    }
}